package com.danger.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.danger.Presenter.ForgetPasswordPresenter;
import com.danger.Presenter.ForgetPasswordPresenterImpl;
import com.danger.View.ForgetPasswordView;

public class ForgetPasswordActivity extends AppCompatActivity implements ForgetPasswordView, View.OnClickListener {

    private EditText forget_password_email_editText;
    private Button forget_password_check_button;
    private ProgressDialog dialog;
    private ForgetPasswordPresenter presenter;
    private Toolbar forget_password_toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        initView();
        initEvent();
    }

    private void initView() {
        dialog = new ProgressDialog(this);
        dialog.setMessage("正在发送邮箱验证...");
        presenter = new ForgetPasswordPresenterImpl(this, this);
        forget_password_email_editText = (EditText) findViewById(R.id.forget_password_email_editText);
        forget_password_check_button = (Button) findViewById(R.id.forget_password_check_button);
        forget_password_toolbar = (Toolbar) findViewById(R.id.forget_password_toolbar);
    }

    private void initEvent() {
        forget_password_check_button.setOnClickListener(this);
        forget_password_toolbar.setTitle("忘记密码");
        forget_password_toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(forget_password_toolbar);
        forget_password_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void showSuccess() {
        Toast.makeText(this, "重置密码请求成功，请到" + forget_password_email_editText.getText().toString() + "邮箱进行密码重置操作", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(ForgetPasswordActivity.this, LoginActivity.class));
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, "重置密码失败:" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget_password_check_button:
                presenter.verifyByEmail(forget_password_email_editText.getText().toString());
                break;
            default:
                break;
        }
    }
}
